package com.hy.ai.controller;

import com.hy.ai.repository.ChatHistoryRepository;
import com.hy.ai.service.ChatService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.List;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class chatController {
    private final ChatService chatService;
    private final ChatHistoryRepository chatHistoryRepository;
    @RequestMapping(value = "/chat",produces = "text/html;charset=utf-8")
    public Flux<String> chat(@RequestParam("prompt") String prompt,
                             @RequestParam("chatId") String chatId,
                             @RequestParam(value = "files" ,required = false) List<MultipartFile> files){
        //保存会话Id
        chatHistoryRepository.save("chat",chatId,"1");
        if(files==null ||files.isEmpty()){
            //没有附件，纯文本
            return chatService.textChat(prompt,chatId);
        }else {
            return chatService.multiModelChat(prompt,chatId,files);
        }
    }

}
